﻿using System;
using System.Collections.Generic;
using System.Linq;
using GPSTracker.API.Helper;

namespace GPSTracker.API.DAL
{
    public class GpsDAL
    {
        public static bool AddGpsData(DataModel.ClientData cd)
        {
            try
            {
                var login = LoginDAL.CheckLoginByTempo(cd.LoginName, cd.LoginPwd, "AddGpsData");

                if (login != null)
                {
                    var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());

                    var gpsData = new DataModel.GpsData();
                    gpsData.TempoID = login.TempoID;
                    gpsData.Longitude = cd.Longitude;
                    gpsData.Latitude = cd.Latitude;
                    gpsData.CreatedDttm = System.DateTime.Now;

                    context.GpsDatas.InsertOnSubmit(gpsData);
                    context.SubmitChanges();

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                var el = new DataModel.ErrorLog();
                el.ErrorMessage = ex.Message;
                el.ErrorForm = "GpsDAL";
                el.ErrorFunction = "AddGpsData";
                CommonDAL.AddErrorLog(el);

                return false;
            }
        }

        public static List<DataModel.VW_GpsData> GetAllGpsData()
        {
            try
            {
                var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());
                return context.VW_GpsDatas.ToList();
            }
            catch (Exception ex)
            {
                var el = new DataModel.ErrorLog();
                el.ErrorMessage = ex.Message;
                el.ErrorForm = "GpsDAL";
                el.ErrorFunction = "GetAllGpsData";
                CommonDAL.AddErrorLog(el);

                return null;
            }
        }

        public static List<DataModel.SP_GetTempoGPSPath_RESTResult> GetTempoGPSPath(DataModel.ClientData cd)
        {
            try
            {
                var login = LoginDAL.CheckLoginByMobile(cd.LoginName, cd.LoginPwd, "GetTempoGPSPath");

                if (login != null)
                {
                    var context = new DataModel.GpsDCDataContext(CommonFx.GetConStr());
                    return context.SP_GetTempoGPSPath_REST(cd.TempoID, Convert.ToDateTime(cd.FromDttm), Convert.ToDateTime(cd.ToDttm)).ToList();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                var el = new DataModel.ErrorLog();
                el.ErrorMessage = ex.Message;
                el.ErrorForm = "GpsDAL";
                el.ErrorFunction = "GetTempoGPSPath";
                CommonDAL.AddErrorLog(el);

                return null;
            }
        }
    }
}
